gusucode.com > VC++ EMF图片浏览器(可读emf、wmf、emz、wmz、png……等)-源码程序 > VC++ EMF图片浏览器(可读emf、wmf、emz、wmz、png……等)-源码程序/code/Src/Client/emfxdemo/emfxdemoDlg.cpp

    //Download by http://www.NewXing.com
// emfxdemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "emfxdemo.h"
#include "emfxdemoDlg.h"
#include SC_INC_EMFLIB(SCEMFdcRenderer.h)

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDC_WND_EMFIMG (IDC_EMF_PICT+5)	

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CemfxdemoDlg dialog

CemfxdemoDlg::CemfxdemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CemfxdemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CemfxdemoDlg)
	m_strEditFile = _T("");
	m_ChkFitImage = FALSE;
	m_ChkUseGDI = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CemfxdemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CemfxdemoDlg)
	DDX_Text(pDX, IDC_EDIT_FILE, m_strEditFile);
	DDX_Check(pDX, IDC_CHK_FIT, m_ChkFitImage);
	DDX_Check(pDX, IDC_CHK_GDIP, m_ChkUseGDI);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CemfxdemoDlg, CDialog)
	//{{AFX_MSG_MAP(CemfxdemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_LOAD, OnBtnLoad)
	ON_BN_CLICKED(IDC_CHK_GDIP, OnChkGdip)
	ON_BN_CLICKED(IDC_CHK_FIT, OnChkFit)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CemfxdemoDlg message handlers

BOOL CemfxdemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	// Create control: the IDC_EMF_PICT is static, hence won't recieve mouse events.
	// So we make it invisible, and use its ractangle (classic old trick).
	DWORD dwStyle = WS_CHILD|WS_VISIBLE|WS_BORDER|WS_CLIPCHILDREN;
	if (!m_CtlImage.Create(NULL, _T("EMFCtlImage"), 
		dwStyle, 
		CRect(10,10,150,50), this, IDC_WND_EMFIMG))
	{
		TRACE0("Could not create image control 1\n");
		return -1;
	}
	{// replace the fake IDC_EMF_PICT by our control
		CWnd* pWnd = GetDlgItem(IDC_EMF_PICT);
		if (!pWnd)
		{
			TRACE0("Could not find IDC_EMF_PICT\n");
			return -1;
		}
		CRect rect;
		pWnd->GetWindowRect(&rect);
		ScreenToClient(&rect);
		m_CtlImage.MoveWindow(&rect);
	}
	m_CtlImage.SCSetEMFDoc(&m_EMFDoc);	  // connect document (source for pages)
	//---let's set some properties, but do not refresh (last parameter to FALSE)
		// enable GDI+, 
	m_CtlImage.SCEnableGDIp(TRUE, FALSE); 
		// change paper color scheme to solid (default is transparent),
		// and paper color to white
	m_CtlImage.SCSetPaperColorStyle(SC_COLOR_RGBVALUE, RGB(255, 255, 255), FALSE);
		// fit image if you want
	//m_CtlImage.SCSetFitMode(SC_FIT_PAGE, FALSE);
		//call other m_CtlImage.SCSet... if you want
	//----
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CemfxdemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CemfxdemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CemfxdemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CemfxdemoDlg::OnBtnLoad() 
{
	CString sFilters(SC_EMFMODE_FILTERS_LOAD);
	CFileDialog dlg(TRUE, NULL, _T("*.emf"), OFN_HIDEREADONLY|OFN_PATHMUSTEXIST,
		(LPCTSTR)sFilters);
	if (dlg.DoModal() != IDOK)
		return;

	SCOnOpenDocument(dlg.GetPathName());	
}

BOOL CemfxdemoDlg::SCOnOpenDocument(LPCTSTR lpszPathName) 
{
	ASSERT(lpszPathName);
	if (!lpszPathName)
		return FALSE;

	// Init sub doc
	UINT uiFileType = m_EMFDoc.SCOpenDocument(lpszPathName);
	if (SC_FTYPE_UKN==(uiFileType & SC_FTYPE_MASK))
	{
		if (SC_FSTATE_NOTFOUND!=(uiFileType & SC_FSTATE_MASK))
		{
			CString strMsg;
			strMsg = CString(_T("Unable to load %s (unsupported file type)\n")) + lpszPathName;
			AfxMessageBox(strMsg);
		}
		return FALSE;
	}
	// Release cache on a per-document basis. The renderer may create private TT fonts
	// if GDI+ refuses to use what's installed, or if it encounters non-TT fonts.
	CSCEMFdcRenderer::SCCleanFontCopies();

	// Display the first page (RTFs/TXTs may produce more than one page.
	// You won't see all of them in this demo)
	m_CtlImage.SCGotoFirstPage();

	// Show the file path
	m_strEditFile = lpszPathName;
	UpdateData(FALSE);
	return TRUE;
}

void CemfxdemoDlg::OnChkGdip() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_CtlImage.SCEnableGDIp(!m_ChkUseGDI);
}

void CemfxdemoDlg::OnChkFit() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_CtlImage.SCSetFitMode((m_ChkFitImage?SC_FIT_PAGE:SC_FIT_NONE));
}

void CemfxdemoDlg::OnDestroy() 
{
	// release cache (or leak)
	CSCEMFdcRenderer::SCCleanFontCopies();
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}